package com.woniuxy.carlife.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.domain.Coupon;
import com.woniuxy.carlife.domain.ServiceRoyalty;
import com.woniuxy.carlife.domain.UserCoupon;
import com.woniuxy.carlife.param.UserIdAndCouponId;
import com.woniuxy.carlife.service.UserCouponService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fx
 * @since 2021-04-12
 */
@RestController
@RequestMapping("//userCoupon")
@Api(tags = "用户拥有优惠券接口信息")
@Slf4j
public class UserCouponController {

    @Resource
    private UserCouponService userCouponService;

    /**
     * 功能描述〈用户添加优惠券〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 17:46
     * @throws
     */
    @ApiOperation(value = "用户添加优惠券",notes = "<span style='color:red;'>用户添加优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "领取优惠券成功"),
            @ApiResponse(code=20001,message = "领取优惠券失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @PostMapping("/ordinary/userconpon")
    public Result saveUserCoupon(@RequestBody @ApiParam UserCoupon userCoupon) throws Exception {
        log.info("saveUserCoupon开始:参数：{}",userCoupon);
        if (userCoupon.getCouponId()==null||
            userCoupon.getCouponId()<=0||
            userCoupon.getUserId()==null||
            userCoupon.getUserId()<=0)
        {
            log.info("saveUserCoupon结束:返回：{}","参数错误");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        Boolean b = userCouponService.saveUserCoupon(userCoupon);
        if (b){
            log.info("saveUserCoupon结束:返回：{}","领取优惠券成功");
            return new Result(true, StatusCode.OK,"领取优惠券成功");
        }
        log.info("saveUserCoupon结束:返回：{}","领取优惠券失败");
        return new Result(false, StatusCode.ERROR,"领取优惠券失败");
    }

    /**
     * 功能描述〈用户查看已拥有优惠券〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 17:47
     * @throws
     */
    @ApiOperation(value = "用户查看已拥有优惠券",notes = "<span style='color:red;'>用户查看已拥有优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询信息成功",response = Coupon.class),
            @ApiResponse(code=20001,message = "查询信息失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=00000,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("/ordinary/usercoupon")
    public Result findUserCouponByUserId(Integer userId){
        log.info("findUserCouponByUserId开始:参数：{}",userId);
        if (userId<=0||userId==null){
            log.info("findUserCouponByUserId结束:返回：{}","参数错误，为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"参数错误，为空或者小于1");
        }
        List<Coupon> userCouponByUserId =
                userCouponService.findUserCouponByUserId(userId);
        if (ObjectUtils.isEmpty(userCouponByUserId)){
            log.info("findUserCouponByUserId结束:返回：{}","查询失败，没有查到数据");
            return new Result(false, StatusCode.NULLDATA,"查询失败，没有查到数据");
        }
        log.info("findUserCouponByUserId结束:返回：{}","userCouponByUserId");
        return new Result(true, StatusCode.OK,"成功获取用户优惠券信息",userCouponByUserId);
    }

    /**
     * 功能描述〈用户查看拥有的有效优惠券〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 17:48
     * @throws
     */
    @ApiOperation(value = "用户查看拥有的有效优惠券",notes = "<span style='color:red;'>用户查看拥有的有效优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询信息成功",response = Coupon.class),
            @ApiResponse(code=20001,message = "查询信息失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=00000,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("/ordinary/usercouponok")
    public Result findUserCouponWithStatusIsOk(Integer userId){
        log.info("findUserCouponWithStatusIsOk开始:参数：{}",userId);
        if (userId<=0||userId==null){
            log.info("findUserCouponWithStatusIsOk结束:返回：{}","参数错误，为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"参数错误，为空或者小于1");
        }
        List<Coupon> userCouponWithStatusIsOk =
                userCouponService.findUserCouponWithStatusIsOk(userId);
        if (ObjectUtils.isEmpty(userCouponWithStatusIsOk)){
            log.info("findUserCouponWithStatusIsOk:返回：{}","查询失败，没有查到数据");
            return new Result(false, StatusCode.NULLDATA,"查询失败，没有查到数据");
        }
        log.info("findUserCouponWithStatusIsOk结束:返回：{}",userCouponWithStatusIsOk);
        return new Result(true, StatusCode.OK,"成功获取用户有效优惠券信息",userCouponWithStatusIsOk);
    }

    /**
     * 功能描述〈用户使用优惠券〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 17:56
     * @throws
     */
    @ApiOperation(value = "用户使用优惠券",notes = "<span style='color:red;'>用户使用优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "使用优惠券成功"),
            @ApiResponse(code=20001,message = "使用优惠券失败"),
            @ApiResponse(code=30001,message = "参数错误"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId",value = "优惠券",dataType = "Integer",paramType = "query",example = "1"),
    })
    @PutMapping("/ordinary/usercoupon")
    public Result updateUserConponStatusByUserIdAndCouponId(@RequestBody @ApiParam UserIdAndCouponId couponId){
        log.info("updateUserConponStatusByUserIdAndCouponId开始:参数：{}",couponId);
        if (couponId.getCouponId()==null||
                couponId.getCouponId()<=0||
                couponId.getUserId()==null||
                couponId.getUserId()<=0)
        {
            log.info("updateUserConponStatusByUserIdAndCouponId结束:返回：{}","传入参数错误");
            return new Result(false, StatusCode.ERROR,"传入参数错误");
        }
        Boolean b = userCouponService.updateUserConponStatusByUserIdAndCouponId(couponId);
        if (b){
            log.info("updateUserConponStatusByUserIdAndCouponId结束:返回：{}","用户成功使用优惠券");
            return new Result(true, StatusCode.OK,"用户成功使用优惠券");
        }
        log.info("updateUserConponStatusByUserIdAndCouponId结束:返回：{}","用户使用优惠券失败");
        return new Result(false, StatusCode.ERROR,"用户使用优惠券失败");
    }
}

